ggplot2 Themes

Author
Affiliation

Calvin J. Chiou

National Chengchi University (NCCU)

Published

May 31, 2024

Introduction

In this document, we will explore various themes and styles available in ggplot2.

library(ggplot2)
library(ggthemes)

# Basic plot
p <- ggplot(mpg, aes(x=displ, y=hwy, color=class)) +
  geom_point() +
  labs(title="Default Theme")

p

Below are examples of applying different themes to your plot:

Theme Minimal

p + theme_minimal() +
  labs(title="Theme Minimal")

Theme Classic

p + theme_dark() +
  labs(title="Theme Dark")

Theme Economist

p + theme_economist() +
  labs(title="Theme Economist")

Theme Economist - White

p + theme_economist_white() +
  labs(title="Theme Economist - White")

Theme Excel

p + theme_excel() +
  labs(title="Theme Excel")

Theme Excel - New

p + theme_excel_new() +
  labs(title="Theme Excel - New")

Theme Highcharts

p + theme_hc() +
  labs(title="Theme Highcharts")

Theme Google Docs

p + theme_gdocs() +
  labs(title="Theme Google Docs")

Theme 538

p + theme_fivethirtyeight() +
  labs(title="Theme 538")

Theme Stata

p + theme_stata() +
  labs(title="Theme Stata")

Theme Wall Street Journal (WSJ)

p + theme_wsj() +
  labs(title="Theme Wall Street Journal")

Theme Pander

p + theme_pander() +
  labs(title="Theme Pander")

Custom Theme

custom_theme <- theme(
  plot.title = element_text(size=14, face="bold", hjust=0.5),
  axis.title = element_text(size=12, face="bold"),
  axis.text = element_text(size=10),
  panel.background = element_rect(fill="white"),
  panel.grid.major = element_line(color="grey", size=0.5),
  panel.grid.minor = element_line(color="lightgrey", size=0.25)
)
Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
ℹ Please use the `linewidth` argument instead.
p + custom_theme + labs(title="Custom Theme")